using System;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace DAL
{
    /// <summary>
    /// 接口IHandleRecordInfo的实现
    /// </summary>
    public class HandleRecordInfo : IDAL.IHandleRecordInfo
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            string strSql = "select count(*) from HandleRecordInfo where Enabled=1 and HandleRecordID = " + Id.ToString();
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name)
        {
            string strSql = "select count(*) from HandleRecordInfo where Enabled=1 and NAME = '" + name.Trim() + "'";
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 增加一条记录
        /// </summary>
        public bool Add(Model.HandleRecordInfo model)
        {
            string strSql = "insert into HandleRecordInfo(SUSPECTPOINTID, FORMERATTRIBUTEID, POSTATTRIBUTEID, ILLEGALSCOPE, UPDATED, UPDATETIME) values(@SuspectPointID, @FormerAttributeID, @PostAttributeID, @IllegalScope, @Updated, @UpdateTime)";
            SqlParameter SuspectPointID = new SqlParameter("SUSPECTPOINTID", SqlDbType.Int); SuspectPointID.Value = model.SuspectPointID;
            SqlParameter FormerAttributeID = new SqlParameter("FORMERATTRIBUTEID", SqlDbType.Int); FormerAttributeID.Value = model.FormerAttributeID;
            SqlParameter PostAttributeID = new SqlParameter("POSTATTRIBUTEID", SqlDbType.Int); PostAttributeID.Value = model.PostAttributeID;
            SqlParameter IllegalScope = new SqlParameter("ILLEGALSCOPE", SqlDbType.NVarChar); IllegalScope.Value = model.IllegalScope;
            SqlParameter Updated = new SqlParameter("UPDATED", SqlDbType.Bit); Updated.Value = model.Updated;
            SqlParameter UpdateTime = new SqlParameter("UPDATETIME", SqlDbType.DateTime); UpdateTime.Value = model.UpdateTime;
            return DBHelperSQL.GetNums(strSql, new SqlParameter[] { SuspectPointID, FormerAttributeID, PostAttributeID, IllegalScope, Updated, UpdateTime }) == 1 ? true : false;
        }

        /// <summary>
        /// 增加一条记录，返回ID
        /// </summary>
        public int Add2(Model.HandleRecordInfo model)
        {
            string strSql = "insert into HandleRecordInfo(SUSPECTPOINTID, FORMERATTRIBUTEID, POSTATTRIBUTEID, ILLEGALSCOPE, UPDATED, UPDATETIME) values(@SuspectPointID, @FormerAttributeID, @PostAttributeID, @IllegalScope, @Updated, @UpdateTime) SELECT @@IDENTITY AS returnName;";
            SqlParameter SuspectPointID = new SqlParameter("SUSPECTPOINTID", SqlDbType.Int); SuspectPointID.Value = model.SuspectPointID;
            SqlParameter FormerAttributeID = new SqlParameter("FORMERATTRIBUTEID", SqlDbType.Int); FormerAttributeID.Value = model.FormerAttributeID;
            SqlParameter PostAttributeID = new SqlParameter("POSTATTRIBUTEID", SqlDbType.Int); PostAttributeID.Value = model.PostAttributeID;
            SqlParameter IllegalScope = new SqlParameter("ILLEGALSCOPE", SqlDbType.NVarChar); IllegalScope.Value = model.IllegalScope;
            SqlParameter Updated = new SqlParameter("UPDATED", SqlDbType.Bit); Updated.Value = model.Updated;
            SqlParameter UpdateTime = new SqlParameter("UPDATETIME", SqlDbType.DateTime); UpdateTime.Value = model.UpdateTime;
            return DBHelperSQL.GetInsertID(strSql, new SqlParameter[] { SuspectPointID, FormerAttributeID, PostAttributeID, IllegalScope, Updated, UpdateTime });
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        public bool Update(Model.HandleRecordInfo model)
        {
            string strSql = "update HandleRecordInfo set SUSPECTPOINTID = @SuspectPointID, FORMERATTRIBUTEID = @FormerAttributeID, POSTATTRIBUTEID = @PostAttributeID, ILLEGALSCOPE = @IllegalScope, UPDATED = @Updated, UPDATETIME = @UpdateTime where HandleRecordID= " + model.HandleRecordID.ToString();
            SqlParameter SuspectPointID = new SqlParameter("SUSPECTPOINTID", SqlDbType.Int); SuspectPointID.Value = model.SuspectPointID;
            SqlParameter FormerAttributeID = new SqlParameter("FORMERATTRIBUTEID", SqlDbType.Int); FormerAttributeID.Value = model.FormerAttributeID;
            SqlParameter PostAttributeID = new SqlParameter("POSTATTRIBUTEID", SqlDbType.Int); PostAttributeID.Value = model.PostAttributeID;
            SqlParameter IllegalScope = new SqlParameter("ILLEGALSCOPE", SqlDbType.NVarChar); IllegalScope.Value = model.IllegalScope;
            SqlParameter Updated = new SqlParameter("UPDATED", SqlDbType.Bit); Updated.Value = model.Updated;
            SqlParameter UpdateTime = new SqlParameter("UPDATETIME", SqlDbType.DateTime); UpdateTime.Value = model.UpdateTime;
            return DBHelperSQL.GetNums(strSql, new SqlParameter[] { SuspectPointID, FormerAttributeID, PostAttributeID, IllegalScope, Updated, UpdateTime }) == 1 ? true : false;
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        public bool Delete(int Id)
        {
            string strSql = "delete from HandleRecordInfo where HandleRecordID = " + Id.ToString();
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.HandleRecordInfo GetModel(int Id)
        {
            string strSql = "select * from HandleRecordInfo where HandleRecordID = " + Id.ToString();
            Model.HandleRecordInfo model = new Model.HandleRecordInfo();
            DataSet ds = DBHelperSQL.GetDataSet(strSql);
            model.HandleRecordID = Id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.HandleRecordID = Convert.ToInt32(ds.Tables[0].Rows[0]["HandleRecordID"]);
                model.SuspectPointID = Convert.ToInt32(ds.Tables[0].Rows[0]["SuspectPointID"]);
                model.FormerAttributeID = Convert.ToInt32(ds.Tables[0].Rows[0]["FormerAttributeID"]);
                model.PostAttributeID = Convert.ToInt32(ds.Tables[0].Rows[0]["PostAttributeID"]);
                model.IllegalScope = Convert.ToString(ds.Tables[0].Rows[0]["IllegalScope"]);
                model.Updated = Convert.ToBoolean(ds.Tables[0].Rows[0]["Updated"]);
                model.UpdateTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["UpdateTime"]);
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.HandleRecordInfo GetModel(string name)
        {
            //string strSql = "select * from HandleRecordInfo where NAME = '" + name.Trim() + "'";
            //Model.HandleRecordInfo model = new Model.HandleRecordInfo();
            //DataSet ds = DBHelperSQL.GetDataSet(strSql);
            //if (ds.Tables[0].Rows.Count > 0)
            //{
                //model.HandleRecordID = Convert.ToInt32(ds.Tables[0].Rows[0]["HandleRecordID"]);
                //model.SuspectPointID = Convert.ToInt32(ds.Tables[0].Rows[0]["SuspectPointID"]);
                //model.FormerAttributeID = Convert.ToInt32(ds.Tables[0].Rows[0]["FormerAttributeID"]);
                //model.PostAttributeID = Convert.ToInt32(ds.Tables[0].Rows[0]["PostAttributeID"]);
                //model.IllegalScope = Convert.ToString(ds.Tables[0].Rows[0]["IllegalScope"]);
                //model.Updated = Convert.ToBoolean(ds.Tables[0].Rows[0]["Updated"]);
                //model.UpdateTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["UpdateTime"]);
                //return model;
            //}
            //else
            //{
                //return null;
            //}
            return null;
        }

        /// <summary>
        /// 获取数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            string strSql = "select * from HandleRecordInfo where Enabled=1 ";
            if (strWhere.Trim() != "")
                strSql += "and " + strWhere;
            return DBHelperSQL.GetDataSet(strSql);
        }
    }
}
